/*
 * Powered By Rubyliu
 * QQ:876181
 * Since 2012 - 2012
 */

package com.bzzoneit.newsys.service.admin.impl;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bzzoneit.newsys.dao.admin.SchedulerMapper;
import com.bzzoneit.newsys.entity.admin.SchedulerJob;
import com.bzzoneit.newsys.service.admin.SchedulerService;
import com.bzzoneit.newsys.utils.Constant;
import com.bzzoneit.newsys.utils.page.Page;
import com.bzzoneit.newsys.utils.scheduler.SchedulerHandle;

/**
 * @author Ruby liu
 * @version 1.0
 * @since 1.0
 */@Service
public class SchedulerServiceImpl implements SchedulerService {
	
	protected static Logger logger =  LoggerFactory.getLogger("service");
	
	@Autowired
	private SchedulerMapper schedulerMapper;
	

	public void insertScheduler(SchedulerJob  scheduler){
		schedulerMapper.insertOne(scheduler);
	}
	public void updateScheduler(SchedulerJob  scheduler){
		schedulerMapper.updateOne(scheduler);
	}
	public void deleteScheduler(long id){
		schedulerMapper.deleteOne(id);
	}
	
	public SchedulerJob selectScheduler(long id){
	  return 	schedulerMapper.selectOne(id);
	}
	
	public Page<SchedulerJob> getSchedulerList(Page<SchedulerJob> page){
		int pg = page.getPageNo();
		int pz = page.getNumPerPage();
		SchedulerJob scheduler=page.getT();
		int offset = 0;
		int limit = 0;
		if (pg > 0)
			offset = (pg - 1) * pz;
		else
			offset = 0;
		limit = pz;
		String orderString="";		
		if(StringUtils.isNotEmpty(page.getOrderBy())){
			orderString=page.getOrderBy();
			if(StringUtils.isNotEmpty(page.getOrder())){
				orderString+=" "+page.getOrder();
			}
			else{
				orderString+=" asc";
			}
			
		}
		else{
			orderString=" id desc"; 
		}	
		scheduler.setOrderString(orderString);
		List<SchedulerJob> result = schedulerMapper.findSchedulerPage(new RowBounds(offset, limit),scheduler);
		page.setResult(result);
		page.setTotalCount(schedulerMapper.findSchedulerPagecount(scheduler));
		return page;
	}
	
	@Override
	public boolean findSysplSchedulerJobByJobName(String jobName) {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public void startScheduler() throws Exception {
		SchedulerHandle schedulerHandle = SchedulerHandle.getInstance();
		List<SchedulerJob> jobList = schedulerMapper.findAllScheduler();
		schedulerHandle.setJobList(jobList);
		String jobIds = "";
		for(SchedulerJob sysplSchedulerJob:jobList){
			String jobId = sysplSchedulerJob.getId()+"";
			jobIds = "".equals(jobIds)?jobId:jobIds+","+jobId;
		}
		schedulerMapper.modifyAllJobStatus(Constant.ENABLE);
		schedulerHandle.startScheduler(true);
		//this.saveLog(LOG_LEVEL_THIRD, SYS_SCHEDULER, "启动调度器", "启动调度器并初始化任务","");
		
	}
	@Override
	public void stopScheduler() {
		SchedulerHandle.getInstance().stopScheduler();
	//	List<SchedulerJob> jobList =schedulerMapper.findAllScheduler();
		
		schedulerMapper.modifyAllJobStatus(Constant.DISABLE);
//		try {
//			this.saveLog(LOG_LEVEL_THIRD, SYS_SCHEDULER, "停止调度器", "停止调度器","");
//		} catch (ApplicationException e) {
//			e.printStackTrace();
//		}	
	}
	@Override
	public void startJob(long jobId) throws Exception {
		SchedulerJob sysplSchedulerJob = schedulerMapper.selectOne(jobId);
		SchedulerHandle.getInstance().startJob(sysplSchedulerJob);
		StringBuffer sb = new StringBuffer();
//		sb.append("启动job:"+SPLIT).append("jobName:"+sysplSchedulerJob.getJobName()+SPLIT)
//		.append("job类描述:"+sysplSchedulerJob.getJobClassDescript()+SPLIT)
//		.append("触发器类型:"+sysplSchedulerJob.getTriggerType()+SPLIT);
		schedulerMapper.modifyJobStatus(jobId, Constant.ENABLE);//更改状态
		
		//this.saveLog(LOG_LEVEL_THIRD, SYS_SCHEDULER, "启动job", sb.toString(),""+jobId);
		
	}
	@Override
	public void stopJob(long jobId, String jobName) {
		SchedulerHandle.getInstance().stopJob(jobName);
		schedulerMapper.modifyJobStatus(jobId,Constant.DISABLE);//更改状态
	//	this.saveLog(LOG_LEVEL_THIRD, SYS_SCHEDULER, "停止job", "停止job:"+jobName,"")	
	}
}